<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 基础表格
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <!-- <el-button
          type="primary"
          class="handle-del mr10"
          @click="delAllSelection"
          >删除</el-button
        > -->
        <!-- <el-select
          v-model="query.address"
          placeholder="地址"
          class="handle-select mr10"
        >
          <el-option key="1" label="广东省" value="广东省"></el-option>
          <el-option key="2" label="湖南省" value="湖南省"></el-option>
        </el-select> -->
        <!-- <el-input
          v-model="query.name"
          placeholder="用户名"
          class="handle-input mr10"
        ></el-input> -->
        <el-button type="primary" @click="handleAdd">添加</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>

        <el-table-column prop="paper" label="纸类"></el-table-column>
        <el-table-column prop="froth" label="泡沫"></el-table-column>

        <el-table-column prop="copper" label="铜铝"></el-table-column>

        <el-table-column prop="plastic" label="塑料"></el-table-column>
        <el-table-column prop="glass" label="玻璃"></el-table-column>
        <el-table-column prop="spinning" label="纺织"></el-table-column>

        <el-table-column prop="iron" label="铁"></el-table-column>

        <el-table-column prop="largeBaggage" label="大件垃圾"></el-table-column>

        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              v-if="scdsPermissions.includes('screen:centersorter:update')"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button type="text" @click="openNewPage(scope.$index, scope.row)"
              >预览</el-button
            >
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              v-if="scdsPermissions.includes('screen:centersorter:delete')"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="editForm" label-width="auto" :rules="rules">
        <el-form-item label="纸类" prop="paper">
          <el-input v-model="editForm.paper"></el-input>
        </el-form-item>
        <el-form-item label="泡沫" prop="froth">
          <el-input v-model="editForm.froth"></el-input>
        </el-form-item>

        <el-form-item label="铜铝" prop="copper">
          <el-input v-model="editForm.copper"></el-input>
        </el-form-item>

        <el-form-item label="塑料">
          <el-input v-model="editForm.plastic"></el-input>
        </el-form-item>

        <el-form-item label="玻璃" prop="glass">
          <el-input v-model="editForm.glass"></el-input>
        </el-form-item>

        <el-form-item label="纺织" prop="spinning">
          <el-input v-model="editForm.spinning"></el-input>
        </el-form-item>

        <el-form-item label="铁" prop="iron">
          <el-input v-model="editForm.iron"></el-input>
        </el-form-item>

        <el-form-item label="大件垃圾" prop="largeBaggage">
          <el-input v-model="editForm.largeBaggage"></el-input>
        </el-form-item>

        <el-form-item label="一周收运量">
          <el-input
            style="width: 120px; margin-bottom: 10px;"
            v-model="editForm.weeklyCapacity.joinTrend[0]"
          ></el-input>
          <el-input
            style="width: 120px; margin-left: 0px;  margin-bottom: 10px;"
            v-model="editForm.weeklyCapacity.joinTrend[1]"
          ></el-input>
          <el-input
            style="width: 120px; margin-left: 0px; margin-bottom: 10px;"
            v-model="editForm.weeklyCapacity.joinTrend[2]"
          ></el-input>
          <el-input
            style="width: 120px; margin-left: 0px; margin-bottom: 10px;"
            v-model="editForm.weeklyCapacity.joinTrend[3]"
          ></el-input>
          <el-input
            style="width: 120px; margin-left: 0px; margin-bottom: 10px;"
            v-model="editForm.weeklyCapacity.joinTrend[4]"
          ></el-input>
          <el-input
            style="width: 120px; margin-left: 0px; margin-bottom: 10px;"
            v-model="editForm.weeklyCapacity.joinTrend[5]"
          ></el-input>
          <el-input
            style="width: 120px; margin-left: 0px; margin-bottom: 10px;"
            v-model="editForm.weeklyCapacity.joinTrend[6]"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelEdit">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 新增弹出框 -->
    <el-dialog title="新增" :visible.sync="addVisible" width="50%">
      <el-form ref="form" :model="addForm" label-width="auto" :rules="rules">
        <el-form-item label="纸类" prop="paper">
          <el-input v-model="addForm.paper"></el-input>
        </el-form-item>
        <el-form-item label="泡沫" prop="froth">
          <el-input v-model="addForm.froth"></el-input>
        </el-form-item>

        <el-form-item label="铜铝" prop="copper">
          <el-input v-model="addForm.copper"></el-input>
        </el-form-item>

        <el-form-item label="塑料">
          <el-input v-model="addForm.plastic"></el-input>
        </el-form-item>

        <el-form-item label="玻璃" prop="glass">
          <el-input v-model="addForm.glass"></el-input>
        </el-form-item>

        <el-form-item label="纺织" prop="spinning">
          <el-input v-model="addForm.spinning"></el-input>
        </el-form-item>

        <el-form-item label="铁" prop="iron">
          <el-input v-model="addForm.iron"></el-input>
        </el-form-item>

        <el-form-item label="大件垃圾" prop="largeBaggage">
          <el-input v-model="addForm.largeBaggage"></el-input>
        </el-form-item>

        <el-form-item label="一周收运量">
          <el-input
            style="width: 120px; margin-bottom: 10px;"
            v-model="addForm.weeklyCapacity.joinTrend[0]"
          ></el-input>
          <el-input
            style="width: 120px; margin-left: 0px;  margin-bottom: 10px;"
            v-model="addForm.weeklyCapacity.joinTrend[1]"
          ></el-input>
          <el-input
            style="width: 120px; margin-left: 0px; margin-bottom: 10px;"
            v-model="addForm.weeklyCapacity.joinTrend[2]"
          ></el-input>
          <el-input
            style="width: 120px; margin-left: 0px; margin-bottom: 10px;"
            v-model="addForm.weeklyCapacity.joinTrend[3]"
          ></el-input>
          <el-input
            style="width: 120px; margin-left: 0px; margin-bottom: 10px;"
            v-model="addForm.weeklyCapacity.joinTrend[4]"
          ></el-input>
          <el-input
            style="width: 120px; margin-left: 0px; margin-bottom: 10px;"
            v-model="addForm.weeklyCapacity.joinTrend[5]"
          ></el-input>
          <el-input
            style="width: 120px; margin-left: 0px; margin-bottom: 10px;"
            v-model="addForm.weeklyCapacity.joinTrend[6]"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "sortingcenterdatascreen",
  data() {
    return {
      scdsPermissions: [],
      rules: {
        paper: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        froth: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        copper: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        plastic: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        glass: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        spinning: [
          { required: true, message: "必填不能为空", trigger: "blur" },
        ],
        iron: [{ required: true, message: "必填不能为空", trigger: "blur" }],
        largeBaggage: [
          { required: true, message: "必填不能为空", trigger: "blur" },
        ],
      },

      query: {
        address: "",
        name: "",
        pageIndex: 1,
        pageSize: 20,
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      addVisible: false,
      pageTotal: 0,
      form: {},
      addForm: {
        paper: "",
        froth: "",
        copper: "",
        plastic: "",
        glass: "",
        spinning: "",
        iron: "",
        largeBaggage: "",
        weeklyCapacity: {
          joinTrend: [],
        },
      },
      editForm: {
        paper: "",
        froth: "",
        copper: "",
        plastic: "",
        glass: "",
        spinning: "",
        iron: "",
        largeBaggage: "",
        weeklyCapacity: {
          joinTrend: [],
        },
      },
      idx: -1,
      id: -1,
    };
  },
  created() {
    this.getData();
    this.scdsPermissions = this.permissions;
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  watch: {
    "$route.path": function(newVal, oldVal) {
      if (newVal == "/sorting-center-data-screen") {
        this.scdsPermissions = this.permissions;
      }
    },
    // 刷新浏览器时触发
    permissions: function(newVal, oldVal) {
      // console.log(newVal, oldVal);
      this.scdsPermissions = newVal;
    },
  },
  methods: {
    // 保存新增
    saveAdd() {
      for (var key in this.addForm) {
        if (this.addForm[key] == "") {
          return this.$message.warning({
            message: "必填项不能为空",
            duration: 1500,
          });
        }
      }
      if (
        this.addForm.weeklyCapacity.joinTrend.length < 7 ||
        this.addForm.weeklyCapacity.joinTrend.includes(undefined)
      ) {
        return this.$message.warning({
          message: "必填项不能为空",
          duration: 1500,
        });
      }

      this.$axios
        .post("/screen/centersorter", {
          ...this.addForm,
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code != 0) {
            return this.$message.error({
              message: res.msg,
              duration: 1500,
            });
          }

          this.addVisible = false;

          this.addForm = {
            paper: "",
            froth: "",
            copper: "",
            plastic: "",
            glass: "",
            spinning: "",
            iron: "",
            largeBaggage: "",
            weeklyCapacity: {
              joinTrend: [],
            },
          };
          this.getData();
        })
        .catch((err) => {});
    },
    // 预览
    openNewPage(index, row) {
      console.log(row);
      // 线上
      window.open(
        `http://vue.bangning.vip/sorting-screen/index.html?id=${row.id}`
      );
      // 本地
      //   window.open(`http://192.168.9.39:8500/index.html?id=${row.id}`);
    },
    getData() {
      this.$axios
        .get("/screen/centersorter/page", {
          params: {
            page: this.query.pageIndex,
            limit: this.query.pageSize,
          },
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code != 0) {
            return this.$message.error({
              message: res.msg,
              duration: 1500,
            });
          }
          this.tableData = res.data.list;
          this.pageTotal = res.data.total;
        })
        .catch((err) => {});
    },
    // 触发搜索按钮
    handleAdd() {
      this.addVisible = true;
    },
    // 删除操作
    handleDelete(index, row) {
      this.ids = [];
      var ids = this.ids;
      ids[0] = row.id;
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      })
        .then(() => {
          this.delete(ids);
        })
        .catch(() => {});
    },
    delete(ids) {
      this.$axios
        .delete("/screen/centersorter", {
          data: ids,
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          this.$message.success("操作成功");
          this.ids = [];
          this.getData();
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delAllSelection() {
      const length = this.multipleSelection.length;
      let str = "";
      this.delList = this.delList.concat(this.multipleSelection);
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + " ";
      }
      this.$message.error(`删除了${str}`);
      this.multipleSelection = [];
    },
    // 编辑操作
    handleEdit(index, row) {
      // this.idx = index;
      // this.form = row;
      console.log(row);
      this.editVisible = true;
      this.getEdit(row.id);
    },
    getEdit(id) {
      this.$axios
        .get(`/screen/centersorter/${id}`)
        .then(({ data: res }) => {
          console.log(res);
          if (res.code != 0) {
            return this.$message.error({
              message: res.msg,
              duration: 1500,
            });
          }
          if (res.data.garbagePercentage) {
            res.data.garbagePercentage = JSON.parse(res.data.garbagePercentage);
          }
          if (res.data.weeklyCapacity) {
            res.data.weeklyCapacity = JSON.parse(res.data.weeklyCapacity);
          }
          this.editForm = {
            id: res.data.id,
            paper: res.data.paper,
            froth: res.data.froth,
            copper: res.data.copper,
            plastic: res.data.plastic,
            glass: res.data.glass,
            spinning: res.data.spinning,
            iron: res.data.iron,
            largeBaggage: res.data.largeBaggage,
            weeklyCapacity: { joinTrend: res.data.weeklyCapacity },
          };
        })
        .catch((err) => {});
    },
    cancelEdit() {
      this.editVisible = false;
    },
    // 保存编辑
    saveEdit() {
      this.$axios
        .put("/screen/centersorter", {
          id: this.editForm.id,
          paper: this.editForm.paper,
          froth: this.editForm.froth,
          copper: this.editForm.copper,
          plastic: this.editForm.plastic,
          glass: this.editForm.glass,
          spinning: this.editForm.spinning,
          iron: this.editForm.iron,
          largeBaggage: this.editForm.largeBaggage,
          weeklyCapacity: this.editForm.weeklyCapacity,
        })
        .then(({ data: res }) => {
          console.log(res);
          if (res.code != 0) {
            return this.$message.error({
              message: res.msg,
              duration: 1500,
            });
          }
          this.editVisible = false;
          this.getData();
        })
        .catch((err) => {});
      // this.editVisible = false;
      // this.$message.success(`修改第 ${this.idx + 1} 行成功`);
      // this.$set(this.tableData, this.idx, this.form);
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
  },
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
